package com.app.micaihu.ad.custom;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.bean.ad.NativeAd;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.UmEventId;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.listener.AutoHeightControllerListener;
import com.app.micaihu.utils.MobclickAgentUtils;
import com.app.micaihu.view.main.game.GameDetailActivity;
import com.app.utils.util.ViewSizeUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.baidu.mobstat.StatService;

import java.util.HashMap;

/**
 * 游戏视频原生自定义View(适用于跳转视频文章内页)
 * Created by linkai on 2017/2/9.
 */
public class GameVideoNativieView extends RelativeLayout implements View.OnClickListener {

    private NativeAd nativeAd;//显示的广告

    /************ 控件 *************/
    private CustomImageView smallImg;
    private TextView title;
    private TextView desc;
    private TextView type;

    private int width = 324;
    private int height = 232;
    private AutoHeightControllerListener autoHeightControllerListener;

    private HashMap<String, Object> mapEvent;

    public GameVideoNativieView(Context context) {
        super(context);
        initView();
    }

    public GameVideoNativieView(Context context, HashMap<String, Object> mapEvent) {
        super(context);
        this.mapEvent = mapEvent;
        initView();
    }

    /**
     * 初始化公共布局
     */
    protected void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.ad_game_video, this);
        this.title = (TextView) findViewById(R.id.ad_title);
        this.smallImg = (CustomImageView) findViewById(R.id.ad_img);
        this.desc = (TextView) findViewById(R.id.ad_desc);
        this.type = (TextView) findViewById(R.id.ad_type);

        ViewSizeUtils.changeViewAuto1080(smallImg, width, height);

        setOnClickListener(this);
    }

    /**
     * 设置即将加载的广告
     *
     * @param nativeAd
     */
    public void setNativeAd(NativeAd nativeAd) {
        if (nativeAd == null) {
            return;
        }
        this.title.setText(nativeAd.getTitle());
        if (nativeAd.getThumbList() != null && nativeAd.getThumbList().length > 0) {
            LoadImageUtils.getInstance().loadImage(this.smallImg, nativeAd.getThumbList()[0]);
        }
        this.desc.setText(nativeAd.getSummary());
        this.type.setText(nativeAd.getTypeDesc());

        this.nativeAd = nativeAd;
        this.setTag("suces");//设置标识表示加载广告成功
    }

    @Override
    public void onClick(View v) {
        if (nativeAd == null || getContext() == null) {
            return;
        }
        if (mapEvent != null) {
            MobclickAgentUtils.event(UmEventId.game_click, mapEvent);
        }
        Intent intent = new Intent(getContext(), GameDetailActivity.class);
        intent.putExtra(Config.Extra.PARAMETER1, nativeAd.getAdId());
        getContext().startActivity(intent);
        StatService.onEvent(getContext(), "057", "文章内页相关新闻上方视频游戏点击", 1);// 统计的点击的次数
    }

}
